//
//  KSOrderSpecTableViewCell.swift
//  Supermarket
//
//  Created by liuxm on 2017/9/28.
//  Copyright © 2017年 kushao. All rights reserved.
//

import UIKit

/// 订单规格
class KSOrderSpecTableViewCell: UITableViewCell {
    
    private var dimTitleLabel = UILabel()
    private var dimLabel = UILabel()
    private var totalLabel = UILabel()
    private var priceLabel = UILabel()
    private var quantityLabel = UILabel()
    /// 产品图片
    lazy var productImageView: UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "Home_Clothes"))
        imageView.frame = CGRect(x: 10, y: 0, width: 90, height: 90)
        
        self.addSubview(imageView)
        return imageView
    }()
    
    /// 说明
    lazy var productInfoLabel: UILabel = {
        let productInfoLabel = UILabel()
        productInfoLabel.font = UIFont.systemFont(ofSize: 15)
        productInfoLabel.textColor = KSColor.kColor_3
        productInfoLabel.numberOfLines = 0
        productInfoLabel.lineBreakMode = .byWordWrapping
        return productInfoLabel
    }()
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        self.selectionStyle = .none
        /// 产品背景色
        let productView = UIView(frame: CGRect(x: 0, y: 0, width: KSConstants.kScreenWidth, height: 110))
        productView.backgroundColor = UIColor.colorWithHexString(hex: "#f9fafb")
        self.addSubview(productView)
        
        /// 产品图片
        productImageView.frame = CGRect(x: 10, y: 10, width: 90, height: 90)
        productView.addSubview(productImageView)
        
        /// 产品简介
        productInfoLabel.frame = CGRect(x: productImageView.frame.size.width + productImageView.frame.origin.x
            + 10, y: productImageView.frame.origin.y, width: KSConstants.kScreenWidth - (productImageView.frame.size.width + productImageView.frame.origin.x) - 20, height: 40)
        productView.addSubview(productInfoLabel)

            /// 尺寸
            dimTitleLabel.font = UIFont.systemFont(ofSize: 15)
            dimTitleLabel.textColor = KSColor.kColor_3
            self.addSubview(dimTitleLabel)
            dimTitleLabel.snp.makeConstraints({ (make) in
                make.top.equalTo(productView.snp.bottom).offset(10)
                make.left.equalTo(15)
            })
            
            totalLabel.font = UIFont.systemFont(ofSize: 15)
            totalLabel.textColor = KSColor.kColorMainRed
            totalLabel.textAlignment = .right
            self.addSubview(totalLabel)
            totalLabel.snp.makeConstraints({ (make) in
                make.centerY.equalTo(dimTitleLabel)
                make.right.equalTo(-15)
            })
            
            /// 单价
            let priceTitleLabel = UILabel()
            priceTitleLabel.text = "单价："
            priceTitleLabel.font = UIFont.systemFont(ofSize: 15)
            priceTitleLabel.textColor = KSColor.kColor_3
            self.addSubview(priceTitleLabel)
            priceTitleLabel.snp.makeConstraints({ (make) in
                make.top.equalTo(dimTitleLabel.snp.bottom).offset(5)
                make.left.equalTo(15)
            })
            
            priceLabel.font = UIFont.systemFont(ofSize: 15)
            priceLabel.textColor = KSColor.kColor_3
            self.addSubview(priceLabel)
            priceLabel.snp.makeConstraints({ (make) in
                make.centerY.equalTo(priceTitleLabel)
                make.left.equalTo(priceTitleLabel.snp.right)
            })
            
            /// 数量
            quantityLabel.font = UIFont.systemFont(ofSize: 15)
            quantityLabel.textColor = KSColor.kColor_3
            quantityLabel.textAlignment = .right
            self.addSubview(quantityLabel)
            quantityLabel.snp.makeConstraints({ (make) in
                make.centerY.equalTo(priceLabel)
                make.right.equalTo(-15)
            })
            
            let lineView = UIView()
            lineView.backgroundColor = KSColor.kColor_line
            self.addSubview(lineView)
            lineView.snp.makeConstraints({ (make) in
                make.left.equalTo(15)
                make.top.equalTo(priceLabel.snp.bottom).offset(5)
                make.height.equalTo(0.5)
            })
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
        // Configure the view for the selected state
    }
    
    func setDataWith(model: KSGetOrderListOrderGoodsModel) {
        productInfoLabel.text = model.goods_name
        
        let skuModel = model.sku_detail![0] as KSGetOrderListDeliverBoxGoodsListSkuDetailsModel
        dimTitleLabel.text = skuModel.name + "：" +  skuModel.value
        
        let totalStr = NSMutableAttributedString(string: "￥" + model.total_price)
        totalStr.addAttribute(NSAttributedStringKey.font, value: UIFont.systemFont(ofSize: 12), range: NSMakeRange(0, 1))
        totalLabel.attributedText = totalStr
        let priceStr = NSMutableAttributedString(string: "￥\(Int(model.selling_price))/件")
        priceStr.addAttribute(NSAttributedStringKey.font, value: UIFont.systemFont(ofSize: 12), range: NSMakeRange(0, 1))
        priceStr.addAttribute(NSAttributedStringKey.foregroundColor, value: KSColor.kColorMainRed, range: NSMakeRange(0, priceStr.length - 2))
        priceLabel.attributedText = priceStr
        let quantityStr = NSMutableAttributedString(string: "X" + model.goods_num)
        quantityStr.addAttribute(NSAttributedStringKey.font, value: UIFont.systemFont(ofSize: 11), range: NSMakeRange(0, 1))
        quantityLabel.attributedText = quantityStr
    }
}
